<?php
require_once (dirname(__FILE__) .'/PtObjectAbstract.php');
class PtProject extends PtObjectAbstract
{
	public $id;
	public $id_owner;
	public $id_cms;
	public $id_product;
	public $name;
	public $description;
	public $active;
	public $is_public;
	public $date_upd;
	public $date_add;
	
	/**
	 * @see ObjectAbstract::$definition
	 */
	public static $definition = array(
		'table'   => 'project',
		'primary' => 'id_project',
		'fields'  => array(
			'id_owner'    => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
			'id_cms'      => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
			'id_product'  => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
			'name'        => array('type' => self::TYPE_STRING, 'validate' => 'isTplName', 'required' => true, 'size' => 128),
			'description' => array('type' => self::TYPE_STRING, 'validate' => 'isString'),
			'active'      => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
			'is_public'   => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
			'date_add'    => array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
			'date_upd'    => array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
		)
	);
	
	protected $styles;
	
	public function __construct($id_project = null)
	{
		parent::__construct($id_project);
		
		if ($this->id)
		{
			$this->styles = array();
			// load current style
			if (!self::$db)
				self::$db = DbAbstract::getInstance();
			
			// default style
			$sql = new PtDbQuery();
			$sql->from('default_style', 'a');
			$sql->where('a.id_cms = '.(int)$this->id_cms.' AND a.active = 1');
			$result = self::$db->executeS($sql, true, false);
			if ($result)
			{
				foreach ($result as $row)
				{
					$row['selector'] = trim($row['selector']);
					$this->styles[$row['selector']] = array();
					$this->styles[$row['selector']] = self::cssToArray($row['style'], $this->styles[$row['selector']]);
				}
			}
			
			// Update with custom style
			$sql = new PtDbQuery();
			$sql->from('custom_style', 'a');
			$sql->where('a.id_project = '.(int)$this->id);
			$result = self::$db->executeS($sql, true, false);
			if ($result)
			{
				foreach ($result as $row)
				{
					$row['selector'] = trim($row['selector']);
					if (empty($this->styles[$row['selector']]))
						$this->styles[$row['selector']] = array();
					$this->styles[$row['selector']] = self::cssToArray($row['style'], $this->styles[$row['selector']]);
				}
			}
		}
	}
	
	public function getCssStyle ($selector = NULL, $attr = NULL)
	{
		if (empty($selector))
			return $this->styles;
			
		if (empty($attr) AND isset($this->styles[$selector]))
			return $this->styles[$selector];
			
		if (!empty($attr) AND isset($this->styles[$selector]))
		{
			$styles = array();
			foreach ($this->styles[$selector] AS $fullAttr => $value)
			{
				if (preg_match('/^'. $attr .'/i', $fullAttr))
				{
					$styles[$fullAttr] = trim($this->styles[$selector][$fullAttr]);
				}
			}
			
			if (count($styles) == 1 AND isset($styles[$attr]))
				return $styles[$attr];
			elseif (!empty($styles))
				return $styles;
		}
			
		return NULL;
	}
	
	public static function cssToArray($style, $result = array())
	{
		if (!is_array($result))
			$result = array();
		
		$style = trim($style, '{');
		$style = trim($style, '}');
		$style = explode(';', $style);
		foreach ($style AS $stylecss)
		{
			if (empty($stylecss)) continue;
			
			$stylecss = explode(':', $stylecss);
			$result[$stylecss[0]] = $stylecss[1];
		}
		
		return $result;
	}
}